package com.sxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxy.controller.common.BaseContext;
import com.sxy.controller.common.R;
import com.sxy.pojo.Orders;
import com.sxy.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 获取订单信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //创建条件查询器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(Orders::getOrderTime);

        //执行业务层方法
        ordersService.page(ordersPage, lqw);

        return R.success(ordersPage);
    }

    /**
     * 后台获取全部订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getByPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        ordersService.page(ordersPage);

        return R.success(ordersPage);
    }
}
